package cn.itsource.Utils;

import cn.itsource.domain.sys_user;
import jdk.internal.dynalink.beans.StaticClass;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * 用户信息工具类
 */
public class UseTools {

    /**
     * 用户信息工具类
     */


        //获取当前登录用户的ID
        public static Long getCurrentUserId(HttpServletRequest request){
            sys_user userInfo = getUserInfo(request);
            return userInfo.getId();
        }

        //获取当前登录用户的真实姓名
        public static String getCurrentUserRealName(HttpServletRequest request){
            sys_user userInfo = getUserInfo(request);
            return userInfo.getRealName();
        }


        //把用户信息存入session
        public static void saveUserSession(HttpServletRequest request, sys_user user){
            HttpSession session = request.getSession();
            System.err.println("sessionId 1 =" + session.getId());
            session.setAttribute(Constants.LOGIN_USER_INFO, user);
        }

        //从session中获取用户信息
        public static sys_user getUserInfo(HttpServletRequest request){
            // 拿到session对象
            HttpSession session = request.getSession();
            // 打印sessionid(每一个session会话对象都有一个sessionid)
            System.err.println("sessionId 2 =" + session.getId());
            return (sys_user) session.getAttribute(Constants.LOGIN_USER_INFO);
        }

}
